<template>
  <div>
    <div>
      <div class="page-title">
          <span>编辑用户</span>
          <a id="back_a"  @click="back()">返回上一页</a>
      </div>
    </div>

    <el-form :model="form" :rules="rules" ref="adminForm" label-width="100px"  v-loading="form.loading">
      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="账号" prop="account">
            <el-input v-model="form.account"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="真实姓名" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="联系方式" prop="mobile">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" @click="saveAdmin()">保存</el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
  </div>
</template>

<script>
    export default {
        name:'AdminEditor',
        data() {
            return {
                isEdit:false,
                form: {
                    account:'',name:'',headImg:'',mobile:'',loading:false,password:'',
                    adminId:0
                },
                rules: {
                    account: [{ required: true, message: "请输入账户",trigger: "blur"}],
                    name:    [{ required: true, message: "请输入真实姓名",trigger: "blur"}],
                    mobile:  [{ required: true, message: "请输入手机号",trigger: "blur"}]
                }
            }
        },
        methods: {
            back(){
                this.$router.push({name:'AdminList'});
            },
            saveAdmin(){
                this.$refs.adminForm.validate(valid => {
                    if (!valid) {
                        return false;
                    }
                    this.form.loading=true;
                    let params = this.form;
                    this.$api.saveAdmin(params).then(response=>{
                        let result = response.data;
                        if(result.code==0){
                            this.$message({  type: 'success',message: '保存成功!'});
                            this.$router.push({name:'AdminList'});
                        }else if(result.code==999){
                            this.$message({  type: 'error',message: '用户已经存在,请重新输入!'});
                        }else{
                            this.$message({  type: 'error',message:result.data});
                        }
                        this.form.loading=false;
                    });
                });
            }
        },
        mounted() {

        },
        created() {
            let params =  this.$route.params;
            if(params&&params.adminId){
                this.isEdit = true;
                //编辑。填充表单
                this.$api.getAdminInfo(params).then(response=>{
                    let result = response.data;
                    if(result.code==0){
                        this.form =  result.data;
                        this.form.adminId = params.adminId;
                    } else {
                        this.$message({  type: 'error',message:response.data.msg});
                    }
                });
            }
        }
    }
</script>
